package example.dao.provider;

import example.pojo.UserInfo;
import org.apache.ibatis.jdbc.SQL;
import java.util.Map;

public class UserInfoDynaSqlProvider {
    public String selectWithParam(final Map<String,Object> params){
        String sql= new SQL(){
            {
                SELECT("*");
                FROM("userInfo");
                if(params.get("userInfo")!=null) {
                    UserInfo userInfo = (UserInfo) params.get("UserInfo");
                    if (userInfo.getUserName() != null && !userInfo.getUserName().equals("")) {
                        WHERE("userName LIKE CONCAT('%',#{userInfo.userName},'%')");
                    }
                }
            }
        }.toString();
        if (params.get("pager")!=null){
            sql += "limit #{pager.firstLimitParam},#{pager.perPageRows}";
        }
        return sql;
    }

    public String count(final Map<String,Object> params){
        return new SQL(){
            {
                SELECT("count(*)");
                FROM("userInfo");
                if(params.get("userInfo")!=null) {
                    UserInfo userInfo = (UserInfo) params.get("UserInfo");
                    if (userInfo.getUserName() != null && !userInfo.getUserName().equals("")) {
                        WHERE("userName LIKE CONCAT('%',#{userInfo.userName},'%')");
                    }
                }
            }
        }.toString();
    }
}
